<?php
	include("library/vars.php");
	
	$uploadDir = 'artwork/';
	
	if(isset($_POST['submit']) && $_FILES['picture']['size'] > 0) {
		// If the form is submited and the artwork is given...
	
		$show_name = addslashes($_POST['show_name']);
		$show_nicename = str_replace('&', 'and', $show_name);
		$show_nicename = strtolower(preg_replace('/\W/','',$show_nicename));
	
		$fileName = $_FILES['picture']['name'];
		$tmpName = $_FILES['picture']['tmp_name'];
		$fileSize = $_FILES['picture']['size'];
		$fileType = $_FILES['picture']['type'];

		// get the file extension first
		$ext      = substr(strrchr($fileName, "."), 1); 
		
		if (!($ext == "png" OR $ext == "jpg" OR $ext == "gif")) {
			header( 'Location: edit_show.php?s=' .$show. '&m=only_images' );
		}
		else {

		// generate the random file name
		$randName = md5(rand() * time());

		// and now we have the unique file name for the upload file
	    $filePath = $uploadDir . $randName . '.' . $ext;

	    // move the files to the specified directory
		// if the upload directory is not writable or
		// something else went wrong $result will be false
	    $result    = move_uploaded_file($tmpName, $filePath);
		if (!$result) {
			echo "Error uploading file";
			exit;
		}
	
		include("library/config.php");
		include("library/opendb.php");
	
		if ($show_nicename==$show) { // Check to see if we changed the show name
			if(!get_magic_quotes_gpc()) {
				$fileName = addslashes($fileName);
				$filePath = addslashes($filePath);
			}

			$query = "UPDATE shows SET show_name='$show_name',show_nicename='$show_nicename',path='$filePath',name='$fileName',size='$fileSize',type='$fileType' WHERE show_nicename='$show'";

			mysql_query($query) or die('Database Error');

			include 'library/closedb.php';
			header( 'Location: index.php?s=' .$show_nicename. '&m=show_updated' );
		}
		else { // If we changed the show name, check to see if there's already a show with that name
	
			$query="SELECT * FROM shows WHERE show_nicename='$show_nicename'";
			$result = mysql_query($query);
			$num=mysql_numrows($result);
	
			if ($num==0) { // If not, edit the show
				if(!get_magic_quotes_gpc()) {
					$fileName = addslashes($fileName);
					$filePath = addslashes($filePath);
				}

				$query = "UPDATE shows SET show_name='$show_name',show_nicename='$show_nicename',path='$filePath',name='$fileName',size='$fileSize',type='$fileType' WHERE show_nicename='$show'";

				mysql_query($query) or die('Database Error');

				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&m=show_updated' );
			} 
			else {
				include 'library/closedb.php';
				header( 'Location: edit_show.php?s=' . $show . '&m=show_already_exists2' );
			}
		}
		}
	}
	
	elseif(isset($_POST['submit'])) { // If the form is submited without artwork
	
		$show_name = addslashes($_POST['show_name']);
		$show_nicename = str_replace('&', 'and', $show_name);
		$show_nicename = strtolower(preg_replace('/\W/','',$show_nicename));
	
		include("library/config.php");
		include("library/opendb.php");
	
		if ($show_nicename==$show) { // Check to see if we changed the show name
			$query = "UPDATE shows SET show_name='$show_name',show_nicename='$show_nicename' WHERE show_nicename='$show'";
			mysql_query($query);

			$query = "UPDATE seasons SET show_ID='$show_nicename' WHERE show_ID='$show'";
			mysql_query($query);

			$query = "UPDATE episodes SET show_ID='$show_nicename' WHERE show_ID='$show'";
			mysql_query($query) or die('Database Error');

			include 'library/closedb.php';
			header( 'Location: index.php?s=' .$show_nicename. '&m=show_updated' );
		}
		else { // If we changed the show name, check to see if there's already a show with that name
			$query="SELECT * FROM shows WHERE show_nicename='$show_nicename'";
			$result = mysql_query($query);
			$num=mysql_numrows($result);
	
			if ($num==0) { // If not, edit the show
				$query = "UPDATE shows SET show_name='$show_name',show_nicename='$show_nicename' WHERE show_nicename='$show'";
				mysql_query($query);

				$query = "UPDATE seasons SET show_ID='$show_nicename' WHERE show_ID='$show'";
				mysql_query($query);

				$query = "UPDATE episodes SET show_ID='$show_nicename' WHERE show_ID='$show'";
				mysql_query($query) or die('Database Error');

				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&m=show_updated' );
			} 
			else {
				include 'library/closedb.php';
				header( 'Location: edit_show.php?s=' . $show . '&m=show_already_exists2' );
			}
		}
	}
	
	else {
		include("library/config.php");
		include("library/opendb.php");

		$query="SELECT * FROM shows WHERE show_nicename='$show'";
		$result=mysql_query($query);

		$i=0;
		$show_ID=mysql_result($result,$i,"id");
		$showname=mysql_result($result,$i,"show_name");
		$imagepath=mysql_result($result,$i,"path");
		$size=mysql_result($result,$i,"size");
		
		include("library/closedb.php");
		include("library/header.php");
?>

<div id="main">
	<div id="seasons">
		<h2><?php echo _("Edit Show"); ?></h2>
	</div>
	
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" id="edit" autocomplete="off" action="" enctype="multipart/form-data">
		<p><label for="show_name"><?php echo _("Show Name"); ?>:</label> <input name="show_name" type="text" id="show_name" class="{required:true}" value="<?=$showname;?>" />
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000" /></p>
			<?php
				// Check to see if the show has artwork
				if ($size == 0) {
					echo '<p style="margin-left:130px;"><img style="border:1px solid #666" src="library/images/no_artwork.png" alt="' . "$showname" . '" width="206"/></p>';
				} 
				else {
					echo '<p style="margin-left:130px;"><img style="border:1px solid #666" src="' . "$imagepath" . '" alt="' . "$showname" . '" width="206"/></p>';
				}
			?>
	
		<p><label for="picture"><?php echo _("Artwork"); ?>: </label><input name="picture" type="file" id="picture" /><br /><small class="info"><?php echo _("200 x 110px fits best"); ?></small></p>
	
		<p class="submit"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Edit Show"); ?>" /> <?php echo _("or"); ?> <a href="index.php?s=<?=$show?>"><?php echo _("Cancel"); ?></a></p>
	</form>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>